<?php namespace App\Http\Services\Wechat;

use App\Doctors;
use App\Hospital;
use App\OrderAddress;
use App\Orders;
use App\Questions;
use App\User;
use App\Userinfo;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use Log;

class WechatModelMsg
{
    public static function sendOrderMsg($billNum,$url = '#'){

        /** @var Orders $thatOrder */
        $thatOrder = Orders::whereOrderNum($billNum)->whereIspay(1)->first();

        if(!$thatOrder)
            return false;

        /** @var User $thatUser */
        $thatUser = User::whereId($thatOrder->user_id)->first();

        if(!$thatUser)
            return false;

        $app = app("wechat");
        $notice = $app->notice;

        $userId = $thatUser->openid;
        if(!$userId || is_null($userId) || $userId == '')
            return false;

        $templateId = 'vnFMFgGUU0axcHM0v9zgFYbC4IKYfx50wp1F7RVsapw';
        $data = array(
            "first"            => "我们已收到您的货款，开始为您制作T恤，请耐心等待呦: )",
            "orderMoneySum"    => $thatOrder->s_amount,
            "orderProductName" => "ZoomYoung个性定制T恤",
            'Remark'           => "如有问题请致电020-66344068或直接在微信留言，小Z将第一时间为您服务！",
        );
        $result = $notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($userId)->send();

        if($result->errmsg == 'ok')
            return true;
        else
            return $result->errmsg;
    }

    public static function sendMsgToManager($userId,$billNum,$url = '#'){

        /** @var Orders $thatOrder */
        $thatOrder = Orders::whereOrderNum($billNum)->first();

        if(!$thatOrder)
            return false;

        $thatOrderAddress = OrderAddress::whereId($thatOrder->address_id)->first();

        /** @var User $thatUser */
        $thatUser = User::whereId($thatOrder->user_id)->first();

        if(!$thatUser)
            return false;

        /** @var Userinfo $thatUserInfo */
        $thatUserInfo = Userinfo::whereUserId($thatUser->id)->first();
        if(!$thatUserInfo)
            return false;

        $app = app("wechat");
        $notice = $app->notice;

//        $userId = "orn9vwAJuQMaGBV5gXHgkebTHP-0";
        if(!$userId || is_null($userId) || $userId == '')
            return false;

        $templateId = 'W0_0yEvA8C3b-dlbOBDymgaEELNv5_298o5edUxzOwA';

        $payType = "未支付";
        if($thatOrder->ispay == 1){
            if($thatOrder->pay_type == "wxpay"){
                $payType = "微信成功支付";
            }else{
                $payType = "支持满20";
            }
        }else{
            if($thatOrder->pay_type == "wxpay"){
                $payType = "原价购买";
            }else{
                $payType = "集支持开始";
            }
        }

        $data = array(
            "first"    => "有人下单啦！【".$payType."】",
            "keyword1" => $thatUserInfo->nickname,
            "keyword2" => "".$thatOrder->created_at,
            "keyword3" => "T恤-".$thatOrderAddress->consignee,
            "keyword4" => $thatOrder->s_amount,
            "keyword5" => $thatOrderAddress ? $thatOrderAddress->telephone : "未知",
            'remark'   => "如有问题请致电020-66344068或直接在微信留言，小Z将第一时间为您服务！",
        );
        $result = $notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($userId)->send();

        if($result->errmsg == 'ok')
            return true;
        else
            return $result->errmsg;
    }
}